home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 11 / Cream of the Crop 11-1.iso / compress / gnucpio.zip / MAKEPATH.C < prev    next >
C/C++ Source or Header  |  1996-01-01  |  8KB  |  305 lines

  1. /* makepath.c -- Ensure that a directory path exists.
  2.    Copyright (C) 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Written by David MacKenzie <djm@gnu.ai.mit.edu> and
  19.    Jim Meyering <meyering@cs.utexas.edu>.  */
  20.  
  21. /* This copy of makepath is almost like the fileutils one, but has
  22.    changes for HPUX CDF's.  Maybe the 2 versions of makepath can
  23.    come together again in the future.  */
  24.  
  25. #ifdef __GNUC__
  26. #define alloca __builtin_alloca
  27. #else
  28. #ifdef HAVE_ALLOCA_H
  29. #include <alloca.h>
  30. #else
  31. #ifdef _AIX
  32.  #pragma alloca
  33. #else
  34. char *alloca ();
  35. #endif
  36. #endif
  37. #endif
  38.  
  39. #include <stdio.h>
  40. #include <sys/types.h>
  41. #include <sys/stat.h>
  42. #ifdef HAVE_UNISTD_H
  43. #include <unistd.h>
  44. #endif
  45. #if !defined(S_ISDIR) && defined(S_IFDIR)
  46. #define    S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
  47. #endif
  48.  
  49. #include <errno.h>
  50. #ifdef STDC_HEADERS
  51. #include <stdlib.h>
  52. #else
  53. extern int errno;
  54. #endif
  55.  
  56. #if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
  57. #include <string.h>
  58. #ifndef index
  59. #define index strchr
  60. #endif
  61. #else
  62. #include <strings.h>
  63. #endif
  64.  
  65. #include "system.h" /* for PC's, uid_t, gid_t, mkdir() */
  66.  
  67. void error ();
  68.  
  69. /* Ensure that the directory ARGPATH exists.
  70.    Remove any trailing slashes from ARGPATH before calling this function.
  71.  
  72.    Make any leading directories that don't already exist, with
  73.    permissions PARENT_MODE.
  74.    If the last element of ARGPATH does not exist, create it as
  75.    a new directory with permissions MODE.
  76.    If OWNER and GROUP are non-negative, make them the UID and GID of
  77.    created directories.
  78.    If VERBOSE_FMT_STRING is nonzero, use it as a printf format
  79.    string for printing a message after successfully making a directory,
  80.    with the name of the directory that was just made as an argument.
  81.  
  82.    Return 0 if ARGPATH exists as a directory with the proper
  83.    ownership and permissions when done, otherwise 1.  */
  84.  
  85. int
  86. make_path (argpath, mode, parent_mode, owner, group, verbose_fmt_string)
  87.      char *argpath;
  88.      int mode;
  89.      int parent_mode;
  90.      uid_t owner;
  91.      gid_t group;
  92.      char *verbose_fmt_string;
  93. {
  94.   char *dirpath;        /* A copy we can scribble NULs on.  */
  95.   struct stat stats;
  96.   int retval = 0;
  97.   int oldmask = umask (0);
  98.   dirpath = alloca (strlen (argpath) + 1);
  99.   strcpy (dirpath, argpath);
  100.  
  101.   if (stat (dirpath, &stats))
  102.     {
  103.       char *slash;
  104.       int tmp_mode;        /* Initial perms for leading dirs.  */
  105.       int re_protect;        /* Should leading dirs be unwritable? */
  106.       struct ptr_list
  107.       {
  108.     char *dirname_end;
  109.     struct ptr_list *next;
  110.       };
  111.       struct ptr_list *p, *leading_dirs = NULL;
  112.  
  113.       /* If leading directories shouldn't be writable or executable,
  114.      or should have set[ug]id or sticky bits set and we are setting
  115.      their owners, we need to fix their permissions after making them.  */
  116.       if (((parent_mode & 0300) != 0300)
  117.       || (owner != (uid_t) -1 && group != (gid_t) -1
  118.           && (parent_mode & 07000) != 0))
  119.     {
  120.       tmp_mode = 0700;
  121.       re_protect = 1;
  122.     }
  123.       else
  124.     {
  125.       tmp_mode = parent_mode;
  126.       re_protect = 0;
  127.     }
  128.  
  129.       slash = dirpath;
  130.       while (*slash == '/')
  131.     slash++;
  132.       while ((slash = index (slash, '/')))
  133.     {
  134. #ifdef HPUX_CDF
  135.       int    iscdf;
  136.       iscdf = 0;
  137. #endif
  138.       *slash = '\0';
  139.       if (stat (dirpath, &stats))
  140.         {
  141. #ifdef HPUX_CDF
  142.           /* If this component of the pathname ends in `+' and is
  143.          followed by 2 `/'s, then this is a CDF.  We remove the
  144.          `+' from the name and create the directory.  Later
  145.          we will "hide" the directory.  */
  146.           if ( (*(slash +1) == '/') && (*(slash -1) == '+') )
  147.         { 
  148.           iscdf = 1;
  149.           *(slash -1) = '\0';
  150.         }
  151. #endif
  152.           if (mkdir (dirpath, tmp_mode))
  153.         {
  154.           error (0, errno, "cannot make directory `%s'", dirpath);
  155.           umask (oldmask);
  156.           return 1;
  157.         }
  158.           else
  159.         {
  160.           if (verbose_fmt_string != NULL)
  161.             error (0, 0, verbose_fmt_string, dirpath);
  162.  
  163.           if (owner != (uid_t) -1 && group != (gid_t) -1
  164.               && chown (dirpath, owner, group)
  165. #ifdef AFS
  166.               && errno != EPERM
  167. #endif
  168.               )
  169.             {
  170.               error (0, errno, "%s", dirpath);
  171.               retval = 1;
  172.             }
  173.           if (re_protect)
  174.             {
  175.               struct ptr_list *new = (struct ptr_list *)
  176.             alloca (sizeof (struct ptr_list));
  177.               new->dirname_end = slash;
  178.               new->next = leading_dirs;
  179.               leading_dirs = new;
  180.             }
  181. #ifdef HPUX_CDF
  182.           if (iscdf)
  183.             {
  184.               /*  If this is a CDF, "hide" the directory by setting
  185.               its hidden/setuid bit.  Also add the `+' back to
  186.               its name (since once it's "hidden" we must refer
  187.               to as `name+' instead of `name').  */
  188.               chmod (dirpath, 04700);
  189.               *(slash - 1) = '+';
  190.             }
  191. #endif
  192.         }
  193.         }
  194.       else if (!S_ISDIR (stats.st_mode))
  195.         {
  196.           error (0, 0, "`%s' exists but is not a directory", dirpath);
  197.           umask (oldmask);
  198.           return 1;
  199.         }
  200.  
  201.       *slash++ = '/';
  202.  
  203.       /* Avoid unnecessary calls to `stat' when given
  204.          pathnames containing multiple adjacent slashes.  */
  205.       while (*slash == '/')
  206.         slash++;
  207.     }
  208.  
  209.       /* We're done making leading directories.
  210.      Make the final component of the path. */
  211.  
  212.       if (mkdir (dirpath, mode))
  213.     {
  214.       /* In some cases, if the final component in dirpath was `.' then we 
  215.          just got an EEXIST error from that last mkdir().  If that's
  216.          the case, ignore it.  */
  217.       if ( (errno != EEXIST) ||
  218.            (stat (dirpath, &stats) != 0) ||
  219.            (!S_ISDIR (stats.st_mode) ) )
  220.         {
  221.           error (0, errno, "cannot make directory `%s'", dirpath);
  222.           umask (oldmask);
  223.           return 1;
  224.         }
  225.     }
  226.       if (verbose_fmt_string != NULL)
  227.     error (0, 0, verbose_fmt_string, dirpath);
  228.  
  229.       if (owner != (uid_t) -1 && group != (gid_t) -1)
  230.     {
  231.       if (chown (dirpath, owner, group)
  232. #ifdef AFS
  233.           && errno != EPERM
  234. #endif
  235.           )
  236.         {
  237.           error (0, errno, "%s", dirpath);
  238.           retval = 1;
  239.         }
  240.     }
  241.       /* chown may have turned off some permission bits we wanted.  */
  242.       if ((mode & 07000) != 0 && chmod (dirpath, mode))
  243.         {
  244.           error (0, errno, "%s", dirpath);
  245.           retval = 1;
  246.         }
  247.  
  248.       /* If the mode for leading directories didn't include owner "wx"
  249.      privileges, we have to reset their protections to the correct
  250.      value.  */
  251.       for (p = leading_dirs; p != NULL; p = p->next)
  252.     {
  253.       *(p->dirname_end) = '\0';
  254. #if 0
  255.       /* cpio always calls make_path with parent mode 0700, so
  256.          we don't have to do this.  If we ever do have to do this,
  257.          we have to stat the directory first to get the setuid
  258.          bit so we don't break HP CDF's.  */
  259.       if (chmod (dirpath, parent_mode))
  260.         {
  261.           error (0, errno, "%s", dirpath);
  262.           retval = 1;
  263.         }
  264. #endif
  265.  
  266.     }
  267.     }
  268.   else
  269.     {
  270.       /* We get here if the entire path already exists.  */
  271.  
  272.       if (!S_ISDIR (stats.st_mode))
  273.     {
  274.       error (0, 0, "`%s' exists but is not a directory", dirpath);
  275.       umask (oldmask);
  276.       return 1;
  277.     }
  278.  
  279.       /* chown must precede chmod because on some systems,
  280.      chown clears the set[ug]id bits for non-superusers,
  281.      resulting in incorrect permissions.
  282.      On System V, users can give away files with chown and then not
  283.      be able to chmod them.  So don't give files away.  */
  284.  
  285.       if (owner != (uid_t) -1 && group != (gid_t) -1
  286.       && chown (dirpath, owner, group)
  287. #ifdef AFS
  288.       && errno != EPERM
  289. #endif
  290.       )
  291.     {
  292.       error (0, errno, "%s", dirpath);
  293.       retval = 1;
  294.     }
  295.       if (chmod (dirpath, mode))
  296.     {
  297.       error (0, errno, "%s", dirpath);
  298.       retval = 1;
  299.     }
  300.     }
  301.  
  302.   umask (oldmask);
  303.   return retval;
  304. }
  305.